package top.hidoctor.auscultation.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import top.hidoctor.auscultation.R;


public class ChooseBiaozhuPop extends PopupWindow implements PopupWindow.OnDismissListener {
    private PopupWindow popupWindow;
    private Context context;

    private final View view;
    private ClickListener listener;

    public void setListener(ClickListener listener) {
        this.listener = listener;
    }

    public ChooseBiaozhuPop(Context context) {
        super(context);
        this.context = context;
        view = initView();
        popupWindow = new PopupWindow(initView(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.popwin_anim_style);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOnDismissListener(this);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }


    private View initView() {
        View view = LayoutInflater.from(context).inflate(R.layout.pop_choose, null);
        final Button btnShoudong = view.findViewById(R.id.btn_shoudong);
        final Button btnZidong = view.findViewById(R.id.btn_zidong);
        final Button cancelBtn = view.findViewById(R.id.cancelBtn);
        final View viewContent = view.findViewById(R.id.view_content);
        btnShoudong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(btnShoudong);
                popupWindow.dismiss();
            }
        });
        btnZidong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(btnZidong);
                popupWindow.dismiss();
            }
        });
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(cancelBtn);
                popupWindow.dismiss();
            }
        });
        viewContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        return view;
    }

    public final void show() {
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
        } else {
            matachData();
            popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        }
    }

    private void matachData() {

    }

    @Override
    public void onDismiss() {

    }


    public interface ClickListener {
        void onClick(View view);
    }
}
